package com.fr.dialog.core;

import com.fr.base.Style;
import com.fr.base.core.GraphHelper;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JScrollBar;
import javax.swing.JViewport;
import javax.swing.Scrollable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ImagePreviewPane extends JComponent
  implements Scrollable
{
  private Image image = null;
  private Style imageStyle = null;
  private int imageWidth = 0;
  private int imageHeight = 0;
  private List changeListenerList = new ArrayList();

  public ImagePreviewPane()
  {
    setToolTipText("View Image");
  }

  public Image getImage()
  {
    return this.image;
  }

  public void setImage(Image paramImage)
  {
    this.image = paramImage;
    if (this.image == null)
    {
      if (getParent() instanceof JViewport)
      {
        JScrollBar localJScrollBar = new JScrollBar(0);
        Dimension localDimension = new Dimension(getSize().width - localJScrollBar.getPreferredSize().height, getSize().height - localJScrollBar.getPreferredSize().height);
        ((JViewport)getParent()).setPreferredSize(localDimension);
        getParent().setSize(localDimension);
        ((JViewport)getParent()).setMinimumSize(localDimension);
        ((JViewport)getParent()).setMaximumSize(localDimension);
      }
    }
    else
    {
      GraphHelper.waitForImage(paramImage);
      this.imageWidth = paramImage.getWidth(null);
      this.imageHeight = paramImage.getHeight(null);
      setToolTipText("Image Size: " + this.imageWidth + "x" + this.imageHeight + "px");
      fireChagneListener();
    }
    revalidate();
  }

  public void paintComponent(Graphics paramGraphics)
  {
    super.paintComponent(paramGraphics);
    if (getImage() != null)
      if (getImageStyle() == null)
        paramGraphics.drawImage(getImage(), 0, 0, this);
      else
        GraphHelper.paintImage(paramGraphics, getWidth(), getHeight(), getImage(), getImageStyle(), getImageStyle().getImageLayout(), -1, -1);
  }

  public Dimension getPreferredSize()
  {
    if (this.image == null)
      return super.getPreferredSize();
    return new Dimension(this.imageWidth, this.imageHeight);
  }

  public void addChangeListener(ChangeListener paramChangeListener)
  {
    this.changeListenerList.add(paramChangeListener);
  }

  private void fireChagneListener()
  {
    ChangeEvent localChangeEvent;
    int i;
    if (this.changeListenerList.size() > 0)
    {
      localChangeEvent = new ChangeEvent(getImage());
      for (i = 0; i < this.changeListenerList.size(); ++i)
        ((ChangeListener)this.changeListenerList.get(i)).stateChanged(localChangeEvent);
    }
  }

  public Dimension getPreferredScrollableViewportSize()
  {
    return getPreferredSize();
  }

  public int getScrollableUnitIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2)
  {
    switch (paramInt1)
    {
    case 1:
      return (paramRectangle.height / 10);
    case 0:
      return (paramRectangle.width / 10);
    }
    throw new IllegalArgumentException("Invalid orientation: " + paramInt1);
  }

  public int getScrollableBlockIncrement(Rectangle paramRectangle, int paramInt1, int paramInt2)
  {
    switch (paramInt1)
    {
    case 1:
      return paramRectangle.height;
    case 0:
      return paramRectangle.width;
    }
    throw new IllegalArgumentException("Invalid orientation: " + paramInt1);
  }

  public boolean getScrollableTracksViewportWidth()
  {
    if (getParent() instanceof JViewport)
      return (getParent().getWidth() > getPreferredSize().width);
    return false;
  }

  public boolean getScrollableTracksViewportHeight()
  {
    if (getParent() instanceof JViewport)
      return (getParent().getHeight() > getPreferredSize().height);
    return false;
  }

  public void setImageStyle(Style paramStyle)
  {
    this.imageStyle = paramStyle;
  }

  public Style getImageStyle()
  {
    return this.imageStyle;
  }
}